//
//  HXHomeTitleButton.swift
//  DemoSinaWeibo
//
//  Created by Mon on 15/12/5.
//  Copyright © 2015年 Mon. All rights reserved.
//

import UIKit

class HXHomeTitleButton: UIButton {
    
    // 使用构造方法封装按钮的属性设置
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    init(title: String) {
        super.init(frame: CGRectZero)
        self.setTitle(title, forState: UIControlState.Normal)
        self.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal)
        self.setImage(UIImage(named: "navigationbar_arrow_down"), forState: UIControlState.Normal)
        self.sizeToFit()
    }
    
    
    override func layoutSubviews() {
        super.layoutSubviews()
        // 交换图片和文字的位置,其实改变的就是控件的x坐标
        titleLabel?.frame.origin.x = 0
        imageView?.frame.origin.x = (titleLabel?.frame.size.width)! + 5
    }
    
    /// 图文并排的方法一
    func setTitleImageFuncOne() {
        // 获得当前按钮的尺寸
        let buttonW = frame.size.width;
        let buttonH = frame.size.height;
        
        // 获得图片的尺寸
        let imageW = self.imageView!.image!.size.width;
        let imageH = self.imageView!.image!.size.height;
        
        // 布局标题的位置
        let titleW = self.titleLabel!.frame.size.width;
        // 计算标题的x值
        let titleX = (buttonW - titleW - imageW - 5) * 0.5;
        // 设置标题尺寸
        self.titleLabel!.frame = CGRectMake(titleX, 0,titleW, buttonH);
        
        // 计算图片的x和y值
        let imageX = titleX + titleW + 5;
        let imageY = (buttonH - imageH) * 0.5;
        // 设置图片的尺寸
        self.imageView!.frame = CGRectMake(imageX, imageY, imageW, imageH);
    }
}
